import axios from 'axios';
import store from '../store';
import vue from 'vue';
// import router from '../router';

// 创建axios实例
const service = axios.create({
  timeout: 5000                  // 请求超时时间
});

// 配置头文件， application/JSON
service.defaults.headers.post['Content-Type'] = 'application/json'
service.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest'
service.defaults.headers.get['X-Requested-With'] = 'XMLHttpRequest'
service.defaults.headers.get['Content-Type'] = 'application/json'
service.defaults.headers.put['X-Requested-With'] = 'XMLHttpRequest'
service.defaults.headers.put['Content-Type'] = 'application/json'
// request拦截器
service.interceptors.request.use(config => {
  // Do something before request is sent
  return config;
}, error => {
  // Do something with request error
  console.log(error); // for debug
  Promise.reject(error);
})

// respone拦截器
service.interceptors.response.use(
  response => response,
  error => {
    console.log('err' + error);// for debug
    vue.$Message.error({
                    message: error.message,
                    duration: 5 * 1000,
                    closable: true
                });
    return Promise.reject(error);
  }
)

export default service;
